﻿using System;
using System.Collections.Generic;
using ACompany.OrderProcessing.Model;

namespace ACompany.OrderProcessing.NHibernateRepository
{
    public class OrderRepository : BaseRepository<Order>, IOrderRepository
    {
        public IList<Order> FindAllOrdersContainingProductby(Guid productId)
        {
            //using (var session = SessionFactory.GetNewSession())
            //{
            //    return session.CreateCriteria(typeof(Order))
            //                          .CreateCriteria("Items")
            //                          .Add(NHibernate.Criterion.Expression.Eq("Product.Id", productId))
            //                          .List<Order>();
            //}

            return SessionFactory.GetCurrentSession().CreateCriteria(typeof(Order))
                                      .CreateCriteria("Items")
                                      .Add(NHibernate.Criterion.Expression.Eq("Product.Id", productId))
                                      .List<Order>();

            // OR

            // Criteria API

            //return SessionFactory.GetCurrentSession().CreateCriteria(typeof(Order))
            //    .CreateCriteria("Items")
            //    .Add(NHibernate.Criterion.Expression.Eq("Product.Id", productId))
            //    .List<Order>();
        }
    }
}
